<?php
namespace Manage\Controller;
use Common\Controller\ManageBaseController;
use Think\Model;
use Common\Model\ArticleModel;
use Common\Model\MemberModel;
use Common\Model\TixianModel;
use Common\Model\JiluModel;

class TixianController extends ManageBaseController{
	
	/*
	 * 提现列表
	 */
	public function lists($ajax=null,$status=null,$page=null,$rows=null) {
		$map = array(); $status_view = array('default'=>'所有','del'=>'已删除','forbid'=>'禁用','allow'=>'正常');
		//查询条件 处理
		$now_status = $status_view['default'];
		if (isset($status) && key_exists($status,JiluModel::$mystat)) { //指定查询状态
			$map['status'] = JiluModel::$mystat[$status];
			$now_status = $status_view[$status];
		}else {
			$map['status'] = array('EGT',0); //默认查询状态为未删除的数据
		}
		if ($page <= 0) {
			$page = 1;
		}
		if ($rows > 0) {
			$listRows = $rows;
		}else {
			$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
		}
		$map['cate'] = 2;
		//筛选条件
		$filter = I('param.');
		
		$member = new MemberModel();
		
		if ($filter['keyword']) {
			$map['sid'] = $member->where("account = '".$filter['keyword']."'")->getField("id");
			$this->assign('keyword', $filter['keyword']);
		}
		/******************/
		if ($ajax==='y') {
			$model = new Model('Jilu');
			$total = $model->where($map)->count(); //总记录数
			$list = $model->where($map)->order('id desc')->page("$page,$listRows")->select();
			//list数据处理
			foreach ($list as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']); 
				$account = $member->where("id = ".$v['sid'])->getField("account");
				$v['account'] = $account;
			}
			unset($v);		
			$list = array("total"=>(int)$total,"rows"=>$list);
			$this->ajaxReturn($list);
		}else {
			$this->assign('_page', $page);
			$this->assign('_rows', $listRows);
			$this->assign('now_status',$now_status); //当前页面筛选的状态
			
			// 记录当前列表页的cookie
			cookie(C('CURRENT_URL_NAME'),$_SERVER['REQUEST_URI']);
			$this->display();
		}
	}

	/*
	 * 审核提现
	 */
	public function state($id) {
		$id = (int)$id;
		$model = new Model();
		$jilu = new JiluModel();
		$member = new MemberModel();

		$model->startTrans();//开启 事物
		
		$mid = $jilu->where("id = ".$id)->getField("sid");
		if (empty($mid)){
			$model->rollback();
			$this->error('提现用户不存在!');
		}
		
		$money = $jilu->where("id = ".$id)->getField("money");
		if (empty($money)){
			$model->rollback();
			$this->error('提现金额不正确!');
		}
		
		if (false === $member->where("id = ".$mid)->setDec("total_money",$money)){//减总余额
			$model->rollback();
			$this->error('减少总余额失败!');
		}

		if (false === $member->where("id = ".$mid)->setDec("ice_money",$money)){//减冻结金额
			$model->rollback();
			$this->error('减少冻结金额失败!');
		}
		
		if (false === $jilu->where('`id`='.$id)->setField('status',1)) {//更新记录状态
			$model->rollback();
			$this->error('更新记录状态失败!');
		}
		
		$model->commit();
		$this->success('更新成功');
	}
}